/*
 * Copyright 2007 National Bioinformatics Network
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package dgbgui.tools;

import java.util.HashSet;
import java.util.Set;

import dgbgui.command.AddQueryCommand;
import dgbgui.command.CommandStack;
import dgbgui.modeldocument.Query;
import dgbgui.modeldocument.QueryResult;
import dgbgui.modeldocument.ResultStore;
import dgbgui.modeldocument.definition.Definition;
import dgbgui.modeldocument.definition.ResultsDefinition;
import dgbgui.modeldocument.primitives.Conceptron;
import dgbgui.modeldocument.primitives.ConceptronType;
import dgbgui.modeldocument.primitives.Primitive;
import dgbgui.selection.SelectionManager;


public class CreateFromResultTool implements Tool {
    
    public void execute() {
        
        
        Set<Object> items = new HashSet<Object>();
        items.addAll(SelectionManager.getInstance().getSelection(ConceptronType.class));
        items.addAll(SelectionManager.getInstance().getSelection(Conceptron.class));
        
        assert(items.size() > 0);

        Set<Primitive> primitives = new HashSet<Primitive>();
        for ( Object obj : items ) {
            assert( obj instanceof Primitive);
            primitives.add((Primitive)((Primitive)obj).clone());
        }
        
        // Create a new Definition from the result. For DefinitionFromResults definitions
        // The Query result is both the definition and result
        QueryResult result = new QueryResult(primitives);
        Definition def = new ResultsDefinition(result);
        Query newQuery = new Query(def);
        ResultStore.getInstance().addResult(def.getdgbQLString(),result);
        
        // The command to add teh new query
        AddQueryCommand command = new AddQueryCommand(newQuery);
        CommandStack.getInstance().execute(command);
        
        
    }
    


}
